package com.zzq.base.awesome.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.zzq.base.awesome.entity.SingleObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Date;

import static com.zzq.base.awesome.entity.CommonData.*;


/**
 * @author running@vip.163.com
 */
@Slf4j()
public class JwtTokenUtils {
    public static JWTVerifier verifier = JWT.require(SingleObject.algorithmHS)
            .withClaim("progem","zzq")
            .withIssuer(JWTISSER)
            .build();
    public static String getJwtToken(String userId) {
        log.info("raise token Id: " + userId);
        return JWTPREFIX +getJwtTokenWithOutBarer(userId);
    }
    public static String getJwtTokenWithOutBarer(String userId) {
        try {
            String token = JWT.create()
                    .withKeyId(userId)
                    .withIssuer(JWTISSER)
                    .withIssuedAt(new Date(System.currentTimeMillis()))
                    .withSubject(userId)
                    .withClaim("progem","zzq")
                    .withClaim(USERNAME,userId)
                    .withExpiresAt(new Date(System.currentTimeMillis() + EXPORESTIME))
                    .sign(SingleObject.algorithmHS);
            return token;
        } catch (JWTCreationException exception) {
            return null;
        }
    }
    public static boolean isJwtOk(String token) {
        DecodedJWT decodedJwt = null;
        try {
            decodedJwt = verifier.verify(token);
        } catch (JWTVerificationException e) {
            e.printStackTrace();
            return false;
        }
        return decodedJwt.getKeyId().equals(decodedJwt.getSubject());
    }

    public static String getUserId(String token) {
        DecodedJWT jwt = null;
        try {
            jwt = JWT.decode(token);
            return jwt.getClaim(USERNAME).asString();
        } catch (JWTDecodeException exception) {
            return null;
        }
    }
    public static String getUserIdS(String token) {
        DecodedJWT jwt = null;
        try {
            jwt = JWT.decode(token);
            return jwt.getSubject();
        } catch (JWTDecodeException exception) {
            return null;
        }
    }
    public static void main(String[] args) {
        String s = getJwtToken("1");
        System.out.println(getUserId(StringUtils.substringAfter(s,JWTPREFIX)));

//        log.info("测试token: " + s);
//        System.out.println(isJwtOk(StringUtils.substringAfter(s, JWTPREFIX)));
//        String sa = "Bearer eyJraWQiOiIxIiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJzdWIiOiIxIiwiaXNzIjoiSldUSVNTRVIiLCJwcm9nZW0iOiJ6enEiLCJ1c2VyTmFtZSI6MSwiZXhwIjoxNTgxMTk3ODA2LCJpYXQiOjE1ODExOTQyMDZ9.9LSFZK27HdAdq2e5xnmzK42fTiEcv3U5XhU6n5QDTt4";
//        System.out.println(isJwtOk(StringUtils.substringAfter(sa, JWTPREFIX)));
    }
}


